﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System.Collections.Generic;
using System.Linq;
using System;

namespace DNA.Mvc.Community.Data.Entity
{
    public class PostRepository : GenericForumRepository<Post>, IPostRepostiory
    {
        public PostRepository() : base() { }

        public PostRepository(ForumDb context) : base(context) { }

        public void Delete(int id, string reason, string deleteBy)
        {
            Post post = Find(id);
            bool isThread = post.IsThread;
            var threadID = post.Thread.ID;
            var forumID = post.Thread.Forum.ID;
            var keepPost = false;

            if (isThread)
            {
                if (post.Thread.Posts.Count(p => !p.IsThread) > 0)
                    keepPost = true;
            }
            else
            {
                if (post.Thread.Posts.Count(p => !p.IsThread && p.ParentID == post.ID) > 0)
                    keepPost = true;
            }

            if (keepPost)
            {
                if (isThread)
                {
                    post.Thread.IsDeleted = true;

                    if (post.Thread.Forum.TotalPosts > 0)
                        post.Thread.Forum.TotalPosts--;
                }
                else
                {
                    if (post.Thread.TotalPosts > 0)
                        post.Thread.TotalPosts--;
                }

                post.IsDeleted = true;
                post.DeleteBy = deleteBy;// User.Identity.Name;
                post.DeletedReason = reason;
                post.Deleted = DateTime.Now;
                Update(post);
            }
            else
            {
                if (isThread)
                {
                    if (post.Thread.Forum.TotalPosts > 0)
                        post.Thread.Forum.TotalPosts--;
                    Context.Threads.Remove(post.Thread);
                }
                else
                {
                    if (post.Thread.TotalPosts > 0)
                        post.Thread.TotalPosts--;

                    Delete(post);
                }
            }
        }

        public IEnumerable<Post> GetThreadPosts(int threadID, int pageIndex, int pageSize, out int totalRecords)
        {
            var skips = pageIndex * pageSize;
            totalRecords = DbSet.Count(t => t.ThreadID == threadID);
            var query = DbSet.Where(t => t.ThreadID == threadID);

            if (skips > 0)
                return query.Skip(skips).Take(pageSize);
            else
                return query.Take(pageSize);
        }

        //public Post Reply(int postID, int parentID, Post post)
        //{
        //    throw new System.NotImplementedException();
        //}

        public void Audit(int id, string userName)
        {
            var post = DbSet.Find(id);
            post.IsApproved = true;
            post.Moderator = userName;

            var parentThread = post.Thread;
            
            if (post.IsThread)
            {
                parentThread.IsApproved = true;
                parentThread.Moderator = userName;
            }

            parentThread.TotalPosts++;
            parentThread.LastPostID = post.ID;
            parentThread.Forum.LastPostID = post.ID;
            parentThread.Forum.TotalPosts++;

            if (IsOwnContext)
                Context.SaveChanges();
        }
    }
}
